package com.youdianstar.app

import android.app.Application
import android.content.Context
import androidx.multidex.MultiDex

import com.youdianstar.app.support.moduleApi
import com.youdianstar.app.common.STRIPE_KEY
import com.youdianstar.app.common.loadsir.EmptyCallback
import com.youdianstar.app.common.loadsir.EmptyRecordCallback
import com.youdianstar.app.common.loadsir.ErrorCallback
import com.youdianstar.app.common.loadsir.LoadingCallback
import com.youdianstar.app.common.util.AppUtil
import com.youdianstar.app.common.util.LogUtil
import com.google.firebase.FirebaseApp
import com.kingja.loadsir.core.LoadSir
import com.youdianstar.app.common.loadsir.EmptyInviteCallback
import com.youdianstar.app.common.loadsir.EmptyInviteRcCallback
import com.stripe.android.PaymentConfiguration
import com.tencent.mmkv.MMKV
import me.jessyan.autosize.AutoSize
import me.jessyan.autosize.AutoSizeConfig
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.startKoin
import org.koin.core.logger.Level


/**
 * File: BrsApp
 * Author: 82149 Create: 2021/9/24 16:21
 * Changes (from 2021/9/24)
 * --------------------------------------------------
 * description:
 * ---------------------------------------------------
 *
 */
class BrsApp : Application() {

    private val modules = arrayListOf(
        moduleApi
    )

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        MultiDex.install(base)
    }

    override fun onCreate() {
        super.onCreate()
        AppUtil.setAppGlobal(this)
        LogUtil.init(BuildConfig.DEBUG)
        MMKV.initialize(this.filesDir.absolutePath + "/mmkv")

        PaymentConfiguration.init(this, STRIPE_KEY)
        FirebaseApp.initializeApp(this)

        initKoin()
        initLoadSir()

        // 屏幕适配
        AutoSize.initCompatMultiProcess(this)
        AutoSizeConfig.getInstance()
            .setBaseOnWidth(true)
            .setUseDeviceSize(true)
            .setExcludeFontScale(true)

    }

    /**
     * Kotlin 注入框架 : Koin
     */
    private fun initKoin(){
        startKoin{
            androidLogger(Level.ERROR)
            androidContext(this@BrsApp)
            modules(modules)
        }
    }

    /**
     * 初始化LoadSir 全局注册
     * LoadSir : 网络加载状态处理框架
     */
    private fun initLoadSir(){
        LoadSir.beginBuilder()
            .addCallback(ErrorCallback())
            .addCallback(EmptyCallback())
            .addCallback(EmptyRecordCallback())
            .addCallback(EmptyInviteCallback())
            .addCallback(EmptyInviteRcCallback())
            .addCallback(LoadingCallback())
            .setDefaultCallback(LoadingCallback::class.java)
            .commit()
    }

}